package com.work.formaldehyde.activity;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activityadapter.CityManageAdapter;
import com.work.formaldehyde.activityadapter.JianCeListAdapter;
import com.work.formaldehyde.model.CityManageModel;
import com.work.formaldehyde.model.ShangPingModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class JianCeListActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private boolean ok = true;

    private ImageView  city_comeback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApiUtils.ActivityIsLogin(this, 0);
        setContentView(R.layout.activity_jiancelist);
        city_comeback = findViewById(R.id.city_comeback);
        city_comeback.setOnClickListener(this);
        if (ApiUtils.isNetworkConnected(JianCeListActivity.this))
            inti();
    }

    private ArrayList<String> new_list_view;
    private ArrayList<CityManageModel.data> new_list_views;
    private ListView city_manage_list;
    public CityManageAdapter myAdapter;

    /**
     * 初始加载视图
     */
    public void Init() {
        city_manage_list = findViewById(R.id.city_manage_list);
        new_list_views = new ArrayList<>();
        new_list_view = new ArrayList<>();
        city_manage_list.setOnItemClickListener(this);
            okHttp_postFromParameters();
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        Init();
    }
    private ShangPingModel shangPingModel;
    public static ArrayList<ShangPingModel.data> list1 = new ArrayList<>();


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.city_comeback:
                onBackPressed();
                break;
            case R.id.adds:
                if (!Url.USER_ID.equals("")) {
                    Intent tocity = new Intent(JianCeListActivity.this, CityCheckActivity.class);
                    startActivity(tocity, ActivityOptions.makeSceneTransitionAnimation(JianCeListActivity.this).toBundle());
                } else {
                    Intent is_login = new Intent(JianCeListActivity.this, LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(JianCeListActivity.this).toBundle());
                    ApiUtils.SetToast(JianCeListActivity.this, "请先登录");
                }
                break;

        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent is_login = new Intent(this, ShangPingActivity.class);
        is_login.putExtra("id", list1.get(i).getId());
        is_login.putExtra("manjian", list1.get(i).getGoods_discount());
        startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }

    private void okHttp_postFromParameters() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
//                    RequestBody formBody = new FormBody.Builder()
//                            .build();
//                    Request request = new Request.Builder().url(Url.CELIANGLIST).post(formBody).build();
//                    okhttp3.Response response = okHttpClient.newCall(request).execute();
//                    String msg = response.body().string();
//                    Message message = new Message();
//                    message.obj = msg;
//                    gethot1.sendMessage(message);
                    Request request = new Request.Builder().get().url(Url.SHANGPINGLIST).build();
                    Call call = okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                        }

                        @Override
                        public void onResponse(Call call, final Response response) {
                            try {
                                final String responseStr;
                                responseStr = response.body().string();
                                Message message = new Message();
                                message.obj = responseStr;
                                gethot1.sendMessage(message);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }

    public Handler gethot1 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        shangPingModel = gson.fromJson(msg.obj.toString(),
                                ShangPingModel.class);
                        if (shangPingModel.getData().size() > list1.size()) {
                            list1.clear();
                            list1.addAll(shangPingModel.getData());
                        }
                        JianCeListAdapter myAdapter = new JianCeListAdapter(list1,
                                JianCeListActivity.this, JianCeListActivity.this);
                        city_manage_list.setAdapter(myAdapter);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}
